package com.example.suuu.project.system.controller;

import com.example.suuu.model.result.ResultVo;
import com.example.suuu.project.system.entity.SuUser;
import com.example.suuu.project.system.entity.vo.UserRoleCode;
import com.example.suuu.project.system.query.SuUserQuery;
import com.example.suuu.project.system.service.impl.SuUserServiceImpl;
import com.example.suuu.tools.excel.ExcelUtils;
import com.example.suuu.tools.excel.Selected;
import com.example.suuu.tools.excel.handler.SuUserWriteHandler;
import com.example.suuu.tools.excel.listener.SuUserExcelListener;
import com.example.suuu.tools.log.LogPrint;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * @author sun
 * @desc 用户表 API接口
 * @date 2023-06-28
 */
@CrossOrigin
@RestController
@RequestMapping("/suUser")
@Api(value = "/suUser", tags = {"用户表 API接口"})
@ApiSupport(author = "sun")
public class SuUserController {

    @Resource
    public SuUserServiceImpl suUserService;

    /**
     * 当前实体对应下拉
     */
    private List<Map<String, String>> paramMap = Selected.SelectedSex();

    @PostMapping("/add")
    @LogPrint("用户表新增")
    @ApiOperation(value = "用户表新增", notes = "用户表新增")
    @ApiOperationSupport(ignoreParameters = {"id"})
    public ResultVo add(@Validated @RequestBody SuUser u) {
        return ResultVo.ResultVoSuccess(suUserService.add(u));
    }

    @PutMapping("/update")
    @LogPrint("用户表修改")
    @ApiOperation(value = "用户表修改", notes = "用户表修改")
    @ApiOperationSupport(ignoreParameters = {})
    public ResultVo update(@Validated @RequestBody SuUser u) {
        return ResultVo.ResultVoSuccess(suUserService.update(u));
    }

    @DeleteMapping("/del/{id}")
    @LogPrint("用户表删除")
    @ApiOperation(value = "用户表删除", notes = "用户表删除")
    public ResultVo del(@Validated @PathVariable(value = "id") Integer id) {
        return ResultVo.ResultVoSuccess(suUserService.del(id));
    }

    @GetMapping("/getlist")
    @LogPrint("用户表查询")
    @ApiOperation(value = "用户表查询", notes = "用户表查询")
    @ApiOperationSupport(ignoreParameters = {})
    public ResultVo get(SuUserQuery query) {
        return ResultVo.ResultVoSuccess(suUserService.pagVo(query));
    }

    @GetMapping("/getById/{id}")
    @LogPrint("用户表id查询")
    @ApiOperation(value = "用户表id查询", notes = "用户表id查询")
    @ApiOperationSupport(ignoreParameters = {})
    public ResultVo getById(@Validated @PathVariable(value = "id") Integer id) {
        return ResultVo.ResultVoSuccess(suUserService.getById(id));
    }

    @PostMapping("/userroleadd")
    @LogPrint("用户角色表新增")
    @ApiOperation(value = "用户角色表新增", notes = "用户角色表新增")
    public ResultVo userroleadd(@Validated @RequestBody UserRoleCode u) {
        return ResultVo.ResultVoSuccess(suUserService.insertBatch(u));
    }

    @GetMapping("/Export")
    @LogPrint("导出")
    @ApiOperation(value = "导出", notes = "导出")
    public void getExport(SuUserQuery query, HttpServletResponse response) {
        ExcelUtils.download((List<SuUser>) suUserService.pagVo(query), response, SuUser.class, Boolean.FALSE, new SuUserWriteHandler(paramMap));
    }

    @PostMapping("/excel")
    @LogPrint("导入")
    @ApiOperation(value = "导入", notes = "导入")
    public ResultVo importFromExcel(@RequestPart @RequestParam("file") MultipartFile file) {
        ExcelUtils.importExcel(file, SuUser.class, new SuUserExcelListener());
        return ResultVo.ResultVoSuccess();
    }

    @GetMapping("/download")
    @LogPrint("下载模板")
    @ApiOperation(value = "下载模板", notes = "下载模板")
    public void getdownload(HttpServletResponse response) {
        ExcelUtils.download(null, response, SuUser.class, Boolean.FALSE, new SuUserWriteHandler(paramMap));
    }
}